package com.ygqh.baby.controller;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.exception.YgHttpException;
import com.ygqh.baby.handler.BiOrderStatisticsHandler;
import com.ygqh.baby.model.ngmm.NgmmOrderInfoResult;
import com.ygqh.baby.model.ngmm.NgmmOrderListResult;
import com.ygqh.baby.model.wdt.WdtResult;
import com.ygqh.baby.model.wdt.qyb.WdtSyncLogistics;
import com.ygqh.baby.quartz.QuartzJobHandler;
import com.ygqh.baby.service.*;
import com.ygqh.baby.service.ngmm.api.NgmmApi;
import com.ygqh.baby.service.ngmm.exception.NgmmException;
import com.ygqh.baby.service.task.*;
import com.ygqh.baby.service.wdtqyb.exception.WdtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private YgTaskService ygTaskService;
    @Autowired
    private WdtService wdtService;
    @Autowired
    private YgBiTaskService ygBiTaskService;
    @Autowired
    private YgUserTaskService ygUserTaskService;
    @Autowired
    private YgWdtTaskService ygWdtTaskService;
    @Autowired
    private YgMonitoringService ygMonitoringService;
    @Autowired
    private YgNgmmTaskService ygNgmmTaskService;
    @Autowired
    private YgLSTaskService ygLSTaskService;
    @Autowired
    private YgBalanceTaskService ygBalanceTaskService;
    @Autowired
    private YgNgmmService ygNgmmService;
    @Autowired
    private YgSubscribeTaskService ygSubscribeTaskService;
    @Autowired
    private YgProductSecondTaskService ygProductSecondTaskService;
    @Autowired
    private WxMsgTaskService wxMsgTaskService;
    @Autowired
    private NgmmApi ngmmApi;
    @Autowired
    private YgGroupBuyTaskService ygGroupBuyTaskService;
    @Value("${channel.scan.limit_time}")
    private String channel_scan_limit_time;
    @Value("${orderStatus4Send.limit_time}")
    private String orderStatus4Send_limit_time;
    @Value("${syncStockOrderTask.limit_time}")
    private String syncStockOrderTask_limit_time;
    @Autowired
    private YgSupplierReconciliationService ygSupplierReconciliationService;
    @Autowired
    private YgCouponTaskService ygCouponTaskService;
    @Autowired
    private YgProductBookTaskService ygProductBookTaskService;
    @Autowired
    private QuartzJobHandler quartzJobHandler;
    @Autowired
    private WdtQybLogisticsService wdtQybLogisticsService;
    @Autowired
    private KidsOrderLogisticsService kidsOrderLogisticsService;
    @Autowired
    private RepairBalanceDataService repairBalanceDataService;
    @Autowired
    private YgWholesalerPrebillRecordService ygWholesalerPrebillRecordService;
    @Autowired
    private YgSearchWordTaskService ygSearchWordTaskService;
    @Autowired
    private BiOrderStatisticsHandler biOrderStatisticsHandler;
    @Autowired
    private YgWdtSkuTaskService ygWdtSkuTaskService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
    }

    @RequestMapping("productStatusTask")
    @ResponseBody
    public void productStatusTask() {
        ygTaskService.productStatusTask();
    }

    @RequestMapping("orderPushTask")
    @ResponseBody
    public String orderPushTask() {
        ygLSTaskService.orderPushTask(5);
        return "0000";
    }

    @RequestMapping("balanceTask")
    @ResponseBody
    public String balanceTask(Integer day) {
        ygTaskService.balanceTask(day);
        return "0000";
    }

    @RequestMapping("preIncomeToBalance")
    @ResponseBody
    public String preIncomeToBalance() {
        ygTaskService.preIncomeToBalance();
        return "0000";
    }

    @RequestMapping("orderStatusTask")
    @ResponseBody
    public String orderStatusTask() {
        ygTaskService.orderStatusForCancelTask();
        return "000000";
    }

    @RequestMapping("sendPayInvitation")
    @ResponseBody
    public String sendPayInvitation() {

        ygTaskService.sendPayInvitation();
        return "000000";

    }

    @RequestMapping("orderStatusForCancelTask")
    @ResponseBody
    public String orderStatusForCancelTask() {

        ygTaskService.orderStatusForCancelTask();
        return "000000";

    }

    @RequestMapping("orderStatusForSuccessTask")
    @ResponseBody
    public String orderStatusForSuccessTask() {

        ygTaskService.orderStatusForSuccessTask();
        return "000000";

    }

    @RequestMapping("productCommentTask")
    @ResponseBody
    public String productCommentTask() {

        ygTaskService.productCommentTask();
        return "productCommentTask";

    }

    @RequestMapping("balanceTaskCheck")
    @ResponseBody
    public String balanceTaskCheck() {
        ygTaskService.balanceTaskCheck();
        return "000000";

    }

    @RequestMapping("queryTradeByNO")
    @ResponseBody
    public String queryTradeByNO(String orderCode) {
        WdtResult queryTradeByNO = wdtService.QueryTradeByNO(orderCode);
        return "000000";

    }

    @RequestMapping("channelScan")
    @ResponseBody
    public String channelScan() {
        ygBiTaskService.channelScan(Integer.parseInt(channel_scan_limit_time));
        return "000000";

    }

    @RequestMapping("calculateAddedUsers")
    @ResponseBody
    public String calculateAddedUsers() {
        ygTaskService.calculateAddedUsers();
        return "000000";

    }

    @RequestMapping("statisticsOrderByDay")
    @ResponseBody
    public String statisticsOrderByDay() {
        biOrderStatisticsHandler.statisticsOrderByDay();
        return "000000";

    }

    @RequestMapping("statisticsOrderNew")
    @ResponseBody
    public String statisticsOrderNew(String... shopNo) {
        ygBiTaskService.statisticsOrderNew(shopNo);
        return "000000";

    }

    @RequestMapping("returnOrderForCompleteTask")
    @ResponseBody
    public String returnOrderForCompleteTask() {
        ygTaskService.returnOrderForCompleteTask();
        return "000000";

    }

    @RequestMapping("returnOrderForCancelTask")
    @ResponseBody
    public String returnOrderForCancelTask() {
        ygTaskService.returnOrderForCancelTask();
        return "000000";

    }

    @RequestMapping("updateProductSaleNumAndCommentNum")
    @ResponseBody
    public String updateProductSaleNumAndCommentNum() {
        ygTaskService.updateProductSaleNumAndCommentNum();
        return "000000";

    }

    @RequestMapping("sendRedPacket")
    @ResponseBody
    public String sendRedPacket() {
        ygTaskService.sendRedPacket();
        return "000000";

    }

    @RequestMapping("employeeToEmployer")
    @ResponseBody
    public String employeeToEmployer() {
        ygTaskService.employeeToEmployer();
        return "000000";

    }

    @RequestMapping("affiliateUserReport")
    @ResponseBody
    public String affiliateUserReport() {
        ygTaskService.affiliateUserReport();
        return "000000";

    }

    @RequestMapping("sendGrowthFundMsg")
    @ResponseBody
    public String sendGrowthFundMsg() {
        ygTaskService.sendGrowthFundMsg();
        return "000000";

    }

    @RequestMapping("groupOrderIncompleteReminder")
    @ResponseBody
    public String groupOrderIncompleteReminder() {
        ygTaskService.groupOrderIncompleteReminder();
        return "000000";

    }

    @RequestMapping("groupOrderComplete")
    @ResponseBody
    public String groupOrderComplete() {
        ygTaskService.groupOrderComplete();
        return "000000";

    }

    @RequestMapping("syncLogistics")
    @ResponseBody
    public String syncLogistics() {
        ygWdtTaskService.syncLogistics(Integer.parseInt(orderStatus4Send_limit_time));
        return "000000";

    }

    @RequestMapping("syncStockIn")
    @ResponseBody
    public String syncStockIn() {
        ygWdtTaskService.syncStockIn(60);
        return "000000";

    }

    @RequestMapping("syncStockOut")
    @ResponseBody
    public String syncStockOut() {
        ygWdtTaskService.syncStockOut(60);
        return "000000";

    }

    @RequestMapping("syncSkuStock")
    @ResponseBody
    public String syncSkuStock() {
        ygWdtTaskService.syncSkuStock();
        return "000000";

    }

    @RequestMapping("syncOrder")
    @ResponseBody
    public String syncOrder() {
        ygWdtTaskService.syncOrder(5);
        return "000000";

    }

    @RequestMapping("loadWdtOrder")
    @ResponseBody
    public String loadWdtOrder() {
        ygWdtTaskService.loadWdtOrder(30);
        return "000000";

    }

    @RequestMapping("monitorPushOrderToWDT")
    @ResponseBody
    public String monitorPushOrderToWDT() {
        ygTaskService.monitorPushOrderToWDT();
        return "000000";

    }

    @RequestMapping("loadLogistics")
    @ResponseBody
    public String loadLogistics() throws WdtException {
        ygWdtTaskService.loadLogistics();
        return "000000";

    }

    @RequestMapping("sendKanJiaInvitation")
    @ResponseBody
    public String sendKanJiaInvitation() {
        ygTaskService.sendKanJiaInvitation();
        return "000000";

    }

    @RequestMapping("sendMallWithOrderExpress")
    @ResponseBody
    public void sendMallWithOrderExpress() {
        ygMonitoringService.sendMallWithOrderExpress(55L, "guohao@yiigoo.com");
    }

    @RequestMapping("sendPreSaleForRemind")
    @ResponseBody
    public void sendPreSaleForRemind() {
        ygBiTaskService.sendPreSaleForRemind();
    }

    @RequestMapping("monitoringSystem")
    @ResponseBody
    public void monitoringSystem() {
        ygMonitoringService.monitoringSystem();
    }

    @RequestMapping("statisticalSkuSaleCount")
    @ResponseBody
    public void statisticalSkuSaleCount() {
        ygBiTaskService.statisticalSkuSaleCount();
    }

    @RequestMapping("statisticsWdtOrder")
    @ResponseBody
    public void statisticsWdtOrder() {
        ygBiTaskService.statisticsWdtOrder();
    }

    /**
     * 同步年糕妈妈订单，一小时执行一次
     */
    @RequestMapping("loadNgmmOrder")
    @ResponseBody
    public void loadNgmmOrder() {
        ygNgmmTaskService.loadNgmmOrder(60);
    }

    /**
     * 同步年糕妈妈订单，一小时执行一次
     *
     * @return
     * @throws NgmmException
     */
    @RequestMapping("orderQueryList")
    @ResponseBody
    public NgmmOrderListResult orderQueryList(Date startTime, Date endTime, int pageNo, int pageSize) throws NgmmException {
        return ngmmApi.orderQueryList(startTime, endTime, pageNo, pageSize, "20", "modified");
    }

    /**
     * 同步年糕妈妈订单，一小时执行一次
     *
     * @throws NgmmException
     */
    @RequestMapping("orderQueryInfo")
    @ResponseBody
    public NgmmOrderInfoResult orderQueryInfo(String orderId) throws NgmmException {
        return ngmmApi.orderQueryInfo(orderId);

    }

    @RequestMapping("getNgmmOrderToSave")
    @ResponseBody
    public NgmmOrderInfoResult getNgmmOrderToSave(String orderId) throws NgmmException {
        NgmmOrderInfoResult result = ngmmApi.orderQueryInfo(orderId);
        ygNgmmService.saveNgmmOrders(result.getOrders());
        return result;
    }

    @RequestMapping("updateNgmmExpress")
    @ResponseBody
    public Message updateNgmmExpress(String orderNo, String expressCode, String expressNo) throws NgmmException {
        ygNgmmService.updateNgmmExpress(orderNo, expressCode, expressNo);
        return Message.success(1);
    }

    @RequestMapping("preBalanceTask")
    @ResponseBody
    public void preBalanceTask() {
        ygBalanceTaskService.preBalanceTask();

    }

    @RequestMapping("doPreToBalance")
    @ResponseBody
    public void doPreToBalance(Long orderId) {
        ygBalanceTaskService.doPreToBalance(orderId);

    }

    @RequestMapping("affiliateUserReportV2")
    @ResponseBody
    public void affiliateUserReportv2() {
        ygBalanceTaskService.affiliateUserReport();

    }

    @RequestMapping("checkBalanceToBalanceTask")
    @ResponseBody
    public void checkBalanceToBalanceTask() {
        ygBalanceTaskService.checkBalanceToBalanceTask();
    }

    @RequestMapping("calculateExtraRewards")
    @ResponseBody
    public void calculateExtraRewards() {
        ygBalanceTaskService.calculateExtraRewards();
        ;
    }

    @RequestMapping("sendSubscribeMsg")
    @ResponseBody
    public void sendSubscribeMsg() {
        ygSubscribeTaskService.sendSubscribeMsg();
    }

    /**
     * 秒杀活动更新商品销售价
     */
    @RequestMapping("changeProductPriceBySecond")
    @ResponseBody
    public void changeProductPriceBySecond() {
        ygProductSecondTaskService.changeProductPriceBySecond();
    }

    /**
     * 发送微信客服图文消息
     */
    @RequestMapping("sendCustomerNewsMsg")
    @ResponseBody
    public void sendCustomerNewsMsg() {
        wxMsgTaskService.sendCustomerNewsMsg();
    }

    /**
     * 每日统计品牌订单数据
     */
    @RequestMapping("statisticsSupplierOrderByDay")
    @ResponseBody
    public void statisticsSupplierOrderByDay() {
        ygBiTaskService.statisticsSupplierOrderByDay();
    }

    /**
     * 每日统计新世界品牌订单数据
     */
    @RequestMapping("statisticsSupplierOrderByDayFromKids")
    @ResponseBody
    public void statisticsSupplierOrderByDayFromKids() {
        ygBiTaskService.statisticsSupplierOrderByDayFromKids();
    }

    /**
     * 统计新世界订单数据
     */
    @RequestMapping("statisticsOrderFromKids")
    @ResponseBody
    public void statisticsOrderFromKids() {
        ygBiTaskService.statisticsOrderFromKids();
    }

    @RequestMapping("groupOrderIncompleteReminderV2")
    @ResponseBody
    public void groupOrderIncompleteReminderV2() {
        ygGroupBuyTaskService.groupOrderIncompleteReminder();
    }

    @RequestMapping("groupOrderCompleteV2")
    @ResponseBody
    public void groupOrderCompleteV2() {
        ygGroupBuyTaskService.groupOrderComplete();
    }

    /**
     * 生成代理邀请码
     */
    @RequestMapping("generateInviteCode")
    @ResponseBody
    public void generateInviteCode() {
        ygBalanceTaskService.generateInviteCode();
    }

    @RequestMapping("notPassCultivationByMonth")
    @ResponseBody
    public void notPassCultivationByMonth() {
        ygBalanceTaskService.notPassCultivationByMonth();
    }

    /**
     * 执行商家对账结算任务
     *
     * @param supplierId
     * @return
     */
    @RequestMapping("addSupplierReconciliation")
    @ResponseBody
    public Message addSupplierReconciliation(Long supplierId) {
        ygSupplierReconciliationService.addSupplierReconciliation(supplierId);
        return Message.success(null);
    }

    /**
     * 新用户下单，反赠推荐用户卡券
     */
    @RequestMapping("giveCardCouponToOrderSource")
    @ResponseBody
    public void giveCardCouponToOrderSource() {
        ygCouponTaskService.giveCardCouponToOrderSource();
    }

    /**
     * 卡券到期提醒（10天、20天后到期）
     *
     * @param
     * @return
     * @author sunshuo
     * @date 2018/7/27 10:47
     */
    @RequestMapping("remindCardCouponOver")
    @ResponseBody
    public void remindCardCouponOver() {
        ygCouponTaskService.remindCardCouponOver();
    }

    /**
     * 定时取消活动记录
     */
    @RequestMapping("cancelRecod")
    @ResponseBody
    public void cancelRecod() {
        ygProductBookTaskService.cancelRecod();
    }

    /**
     */
    @RequestMapping("trustoneAddOrder")
    @ResponseBody
    public void trustoneAddOrder(String orderId) throws YgHttpException {
        quartzJobHandler.trustoneAddOrder(orderId);
    }

    /**
     */
    @RequestMapping("loadKidsLogistics")
    @ResponseBody
    public void loadKidsLogisticsTest(int id) throws YgHttpException {
        List<WdtSyncLogistics> trades = wdtQybLogisticsService.findKidsListAfterId(id);
        kidsOrderLogisticsService.loadKidsLogistics(trades);
    }

    @RequestMapping("repairPreBalanceData")
    @ResponseBody
    public String repairPreBalanceData(Long orderId) {
        repairBalanceDataService.repairPreBalanceData(orderId);
        return "ok";
    }

    @RequestMapping("wholesalerPrebillJob")
    @ResponseBody
    public Message wholesalerPrebillJob(Long id) {
        ygWholesalerPrebillRecordService.executeWholesalerPrebillJob(id);
        return Message.success(null);
    }

    /**
     * 搜索词检查
     */
    @RequestMapping("checkSearchWord")
    @ResponseBody
    public void checkSearchWord() {
        ygSearchWordTaskService.checkSearchWord();
    }

    /**
     * 优惠券过期提醒
     */
    @RequestMapping("remindCouponOver")
    @ResponseBody
    public Message remindCouponOver() {
        ygCouponTaskService.remindCouponOver();
        return Message.success(null);
    }

    /**
     * 女王过期提醒
     *
     * @return
     */
    @RequestMapping("checkVipUserValid")
    @ResponseBody
    public Message checkVipUserValid() {
        ygUserTaskService.checkVipUserValid();
        return Message.success(null);
    }

    /**
     * 执行秒杀活动任务
     *
     * @param secondId 活动id
     * @param taskType 当前任务进度 (Start, End)
     * @return
     */
    @RequestMapping("doProductSecondJob")
    @ResponseBody
    public Message doProductSecondJob(@RequestParam Long secondId, @RequestParam String taskType) {
        ygProductSecondTaskService.doProductSecondJob(secondId, taskType);
        return Message.success(null);
    }

    /**
     * 同步所有SKU库存
     */
    @RequestMapping("syncAllSkuStock")
    @ResponseBody
    public Message syncAllSkuStock() {
        ygWdtSkuTaskService.syncAllSkuStock();
        return Message.success(null);
    }

    /**
     * 统计旺店通销售数据并发送邮件
     */
    @RequestMapping("sendWDTSalesReportMail")
    @ResponseBody
    public Message sendWDTSalesReportMail() {
        ygWdtSkuTaskService.sendWDTSalesReportMail();
        return Message.success(null);
    }
}
